package com.kurumi.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kurumi.bo.PageParam;
import com.kurumi.mapper.UsersMapper;
import com.kurumi.pojo.Users;
import com.kurumi.service.UserService;

@Service
public class UserServiceImpl implements UserService {

	@Autowired
	private UsersMapper usersMapper;
	

	@Transactional(readOnly = true)
	@Override
	public PageInfo<Map<String,Object>> getUsersPageInfo(HttpServletRequest request, PageParam pageParam) {
		PageHelper.startPage(pageParam.getPage(), pageParam.getLimit());

		Map<String, Object> params = new HashMap<>();

		String start = request.getParameter("start");
		if (StringUtils.isNotEmpty(start)) {
			params.put("start", start);
		}

		String end = request.getParameter("end");
		if (StringUtils.isNotEmpty(end)) {
			params.put("end", end);
		}

		String wxNumber = request.getParameter("wxNumber");
		if (StringUtils.isNotEmpty(wxNumber)) {
			params.put("wxNumber", wxNumber);
		}

		String phone = request.getParameter("phone");
		if (StringUtils.isNotEmpty(phone)) {
			params.put("phone", phone);
		}
		

		List<Map<String,Object>> userList = usersMapper.selectUsersByParams(params);
		PageInfo<Map<String,Object>> objectPageInfo = new PageInfo<>(userList);
		return objectPageInfo;
	}

	@Transactional(readOnly = true)
	@Override
	public Users getUserById(Integer userId) {
		Users user = usersMapper.selectByPrimaryKey(userId);
		
		return user;
	}

	
	@Transactional(propagation = Propagation.REQUIRED)
	@Override
	public void deleteUserById(Integer id) {
		
		usersMapper.deleteByPrimaryKey(id);
	}

}
